package com.decheng.dao.impl;

import com.decheng.dao.BookMapper;
import com.decheng.pojo.Book;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

/**
 * @Author 孔德成
 * @Slogan 致敬大师，致敬未来的你
 * @date 2021/6/28
 */

//@Repository
public class BookDaoImpl implements BookMapper {

    private final JdbcTemplate jdbcTemplate;

    public BookDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 分页查询所有书籍
     *
     * @param currentPage
     * @param PageSize
     */
    @Override
    public List<Book> selectListByLimit(Integer currentPage, Integer PageSize) {
        Integer offSet = (currentPage - 1) * PageSize;
        String sql = "select * from books limit ?, ?";
        RowMapper<Book> rowMapper = new BeanPropertyRowMapper<>(Book.class);
        return jdbcTemplate.query(sql, rowMapper, offSet, PageSize);
    }

    /**
     * 查询书籍数量
     *
     * @return
     */
    @Override
    public Long count() {
        String sql = "select count(1) from books";
        return jdbcTemplate.queryForObject(sql, Long.class);
    }
}
